package com.photoeditor.demo.ui.widget.image.edit;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.photoeditor.R;
import com.photoeditor.demo.ui.activity.edit.EditorImageActivity;
import com.photoeditor.demo.ui.widget.RotationImageView;

/**
 * 编辑旋转的View
 */
public class RotateBarView extends LinearLayout implements IDealInit {

    private RotationImageView mImageView;

    private EditorImageActivity mActivity;

    private MicroSeekBar mMicroSeekBar;

    private ImageView mRotateLeft;
    private ImageView mRotateBottom;

    public RotateBarView(Context context) {
        this(context, null);
    }

    public RotateBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mActivity = (EditorImageActivity) context;
    }

    public void setRotationImageView(RotationImageView view) {
        mImageView = view;
    }

    @Override
    public void init() {
        mRotateLeft = (ImageView) this.findViewById(R.id.rotate_left);
        mRotateBottom = (ImageView) this.findViewById(R.id.rotate_bottom);
        mMicroSeekBar = (MicroSeekBar) this.findViewById(R.id.seekbar);
//		ImageView btnRotateRight = (ImageView)this.findViewById(R.id.image_edit_tool_rotate_right);
//		ImageView btnRotateBottom = (ImageView)this.findViewById(R.id.rotate_bottom);
        mMicroSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(MicroSeekBar seekBar, float degree, boolean fromUser) {
                mActivity.setConfirmEnable(true);
                mImageView.setMicroRotation(degree);
            }

            @Override
            public void onStartTrackingTouch(MicroSeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(MicroSeekBar seekBar) {

            }
        });
        OnClickListener rotateClickListener = new OnClickListener() {

            @Override
            public void onClick(View v) {
                int id = v.getId();
                mActivity.setConfirmEnable(true);
                if (id == R.id.rotate_left) {
                    mImageView.rotationImageView(-90);
                } else if (id == R.id.rotate_bottom) {
                    mImageView.flipImageView(true);
                }

            }
        };
        mRotateLeft.setOnClickListener(rotateClickListener);
        mRotateBottom.setOnClickListener(rotateClickListener);
        initView();
    }

    public void restore() {
        mMicroSeekBar.resetProgress();
        mImageView.restoreImageView();
    }

    private void initView() {
        setBackgroundResource(R.drawable.image_edit_sencond_bg);
        mRotateLeft.setImageResource(R.drawable.ic_tool_rotate);
        mRotateBottom.setImageResource(R.drawable.ic_tool_rotate_mirror);
    }
}
